﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Diagnostics;

namespace PrattParsing
{
    [DebuggerNonUserCode]
    public abstract class Lexer : IScanner
    {
        protected string input;
        protected int pos;

        protected char cur;

        protected int line, col;
        private TextPos storedPos;

        public Lexer(string input)
        {
            this.input = input;
            this.pos = 0;

            next();
        }

        [DebuggerNonUserCode]
        protected bool whitespace(char c)
        {
            return c == ' ' || c == '\t' || c == '\n' || c == '\r';
        }

        protected void next()
        {
            if (pos < input.Length)
            {
                cur = input[pos];
                if (cur == '\n')
                {
                    line++;
                    col = 0;
                }
                else
                    col++;
                pos++;
            }
            else cur = '\0';
        }

        [DebuggerNonUserCode]
        protected TextPos makePos(int len)
        {
            return new TextPos(pos, line, col, len);
        }
        [DebuggerNonUserCode]
        protected void setPos()
        {
            storedPos = makePos(-1);
        }
        [DebuggerNonUserCode]
        protected TextPos getPos()
        {
            storedPos.len = pos - storedPos.pos;
            return storedPos;
        }

        public abstract Token getToken();
    }
}
